layui.use(['layer', 'form', 'table', 'admin','laydate', 'ax'], function () {
    //var $ = layui.$;
    var $= layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    /**
     * 系统管理--评价管理
     */
    var OBJ = {
        tableId: "pickingTable",    //表格id
        condition: {
            pickingNo: "",
            orderNo:""
        }
    };
//渲染时间选择框
    laydate.render({
        elem: '#createTime',
        range: true,
        max: Feng.currentDate()
    });
    /**
     * 初始化表格的列
     */
    OBJ.initColumn = function () {
        return [[
            /*{type: 'checkbox'},*/
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'sapNo', sort: true, title: '领料单号'},
            {field: 'orderNo', sort: true, title: '报事单号'},
            {field: 'materialName', sort: true, title: '缺货物料'},
            {field: 'materialNumber', sort: true, title: '缺货数量'},
            {field: 'createTime', sort: true, title: '提交时间'},
            //{align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    OBJ.search = function () {
        var queryData = {};
        queryData['pickingNo'] = $("#pickingNo").val().replace(/\s+/g,"");
        queryData['orderNo'] = $("#orderNo").val().replace(/\s+/g,"");
        //queryData['shopAdr'] = $("#shopAdr").val().replace(/\s+/g,"");
        queryData['createTime'] = $("#createTime").val();
        queryData['materialDescribe'] = $("#materialDescribe").val();
        table.reload(OBJ.tableId,{
            page:{
                curr:1
            },
            where: queryData
        })
    };

    /**
     * 弹出添加角色
     */
    OBJ.openAddRole = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '新增工单',
            content: Feng.ctxPath + '/ticket/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Role.tableId);
            }
        });
    };
    /**
     * 点击详情
     *
     * @param data 点击按钮时候的行数据
     */
    OBJ.onDetailPayment = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            area: '1000px',
            title: '缴费详情',
            content: Feng.ctxPath + '/payment/payment_detail?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(OBJ.tableId);
            }
        });
    };


    /**
     * 导出excel按钮
     */
    OBJ.exportExcel = function () {
        var checkRows = table.checkStatus(OBJ.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    //初始化物料下拉框
    function initSelect(){
        //初始化物料下拉框
        var ajax = new $ax(Feng.ctxPath + "/orderPicking/materialData/" );
        var result = ajax.start();
        var list = result;    //返回的数据
        var server = document.getElementById("materialDescribe"); //server为select定义的id
        for(var p in list) {
            var option = document.createElement("option");  // 创建添加option属性
            option.setAttribute("value", p); // 给option的value添加值
            option.innerText = list[p];     // 打印option对应的纯文本
            server.appendChild(option);           //给select添加option子标签
        }
        form.render("select");
    }


    form.render("select");
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + OBJ.tableId,
        url: Feng.ctxPath + '/picking/list',
        page: {
            limits:[10,20,30,50,100,'ALL']
        },
        height: "full-158",
        title:"缺货清单管理",
        cellMinWidth: 100,
        cols: OBJ.initColumn(),
        done:function (res,curr,count) {
            initSelect()
            if (this.limit ==count){
                $(".layui-laypage-limits select").find("option[value='ALL']").attr("selected",true);
            }
            $(".layui-laypage-limits select").find("option[value='ALL']").attr("value",count).text('全部');
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        OBJ.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        OBJ.openAddRole();
    });
    // 导出excel
    $('#btnExp').click(function () {
        OBJ.exportExcel();
    });


    // 工具条点击事件
    table.on('tool(' + OBJ.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'dispatch') {
            OBJ.onDispatchTicket(data);
        } else if (layEvent === 'delete') {
            OBJ.onDeleteTicket(data);
        } else if (layEvent === 'detail') {
            OBJ.onDetailPayment(data);
        }
    });
    //导出全部
    $('#exportAll').on('click', function(){
        var $ = layui.jquery;
        var excel = layui.excel;

        var pickingNo = $("#pickingNo").val().replace(/\s+/g,"");
        var orderNo = $("#orderNo").val().replace(/\s+/g,"");
        var createTime = $("#createTime").val();
        var materialDescribe = $("#materialDescribe").val();
        var shopAdr = "";

        $.ajax({
            url: Feng.ctxPath + '/orderPicking/export?pickingNo='+pickingNo+'&orderNo='+orderNo+'&shopAdr='+shopAdr+'&createTime='+createTime+'&materialDescribe='+materialDescribe+'&lackMaterialsFlag=1',
            dataType: 'json',

            success: function(wa) {
                // layer.close(loading);
                // 假如返回的 res.data 是需要导出的列表数据
                console.log(wa);//dataSource
                // 1. 数组头部新增表头
                // 3. 执行导出函数，系统会弹出弹框
                table.exportFile(tableResult.config.id, wa, 'xls')
            },
            error:function(res){
                layer.close(loading);
                layer.msg(res.msg);
            }
        });

    });
});
